import http from "@/http"
import {ImplementationItem, softwarePlan,softwarePlanListParam, ExportRequest, ExportFieldInfo, SoftwarePricingPlanBatchCopyParam} from '@/api/softwarePricingPlan/type.ts'
// 根据仓库id,数量，项目金额查询价格明细
export const getDetailApi = (warehouseId:number, num:number, money:number) =>{
    return http.get("/softwarePricingPlan/getDetail", {
        warehouseId,num,money
    })
}

// 软件核价单分页列表查询
export const querySoftwarePlanListApi = (data: softwarePlanListParam) => {
    return http.get("/softwarePricingPlan/querySoftwarePlanList", data)
}

// 软件核价单删除接口
export const deleteApi = (id:number) => {
    return http.delete("/softwarePricingPlan/deleteSoftwarePricingPlan/"+id)
}

// 保存软件报价单
export const submitSoftwarePricingPlanApi = (data: softwarePlan) => {
    return http.post("/softwarePricingPlan/saveSoftwarePlan", data)
}

// 修改软件报价单
export const submitUpdateSoftwarePlan = (data: softwarePlan) => {
    return http.put("/softwarePricingPlan/updateSoftwarePlan", data)
}

// 查询软件报价单详情
export const getSoftwarePlanApi = (id:string) => {
    return http.get("/softwarePricingPlan/getSoftwarePlan/"+id)
}

// 删除软件核价方案
export const deleteSoftwarePricingPlan = (id: number) => {
  return http.delete('/softwarePricingPlan/deleteSoftwarePricingPlan/' + id)
}

// 更新软件核价状态
export const updateStatusApi = (params: { id: number; activate: number }) => {
  return http.put('/softwarePricingPlan/updateStatus', params)
}

// 导入软件核价方案
export const importSoftwarePricingPlanApi = (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return http.post('/softwarePricingPlan/importSoftwarePricingPlan', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 导出软件核价方案（GET请求，使用URL直接下载）
export const exportSoftwarePricingPlanApi = (params: softwarePlanListParam) => {
    // 构建查询参数
    const queryParams = new URLSearchParams()
    if (params.projectId !== undefined) queryParams.append('projectId', params.projectId.toString())
    if (params.repositoryId !== undefined) queryParams.append('repositoryId', params.repositoryId.toString())
    if (params.activate !== undefined) queryParams.append('activate', params.activate.toString())
    
    const url = `/api/softwarePricingPlan/exportSoftwarePricingPlan?${queryParams.toString()}`
    window.location.href = url
    return Promise.resolve()
}

// 下载软件核价方案模板（GET请求，使用URL直接下载）
export const downloadSoftwarePricingPlanTemplateApi = () => {
    const url = `/api/softwarePricingPlan/downloadTemplate`
    window.location.href = url
    return Promise.resolve()
}

// 自定义字段导出软件核价方案（POST请求，手动处理blob下载）
export const exportCustomSoftwarePricingPlanApi = (data: ExportRequest) => {
    return http.post("/softwarePricingPlan/exportCustom", data, { responseType: 'blob' }).then(response => {
        // 创建Blob链接并触发下载
        const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = '软件核价方案导出.xlsx'
        link.click()
        window.URL.revokeObjectURL(link.href)
        return Promise.resolve()
    })
}

// 获取可导出字段列表
export const getExportFieldsApi = () => {
    return http.get('/softwarePricingPlan/getExportFields')
}

// 批量更新软件核价状态
export const batchUpdateStatusApi = (params: { ids: number[], activate: number }) => {
    return http.put("/softwarePricingPlan/batchUpdateStatus", params)
}

// 批量复制软件核价方案
export const batchCopyApi = (data: SoftwarePricingPlanBatchCopyParam) => {
    return http.post("/softwarePricingPlan/batchCopy", data)
}
